package com.nb.mapper;

import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateWrapper;
import com.nb.dto.UserQueryDTO;
import com.nb.entity.User;
import com.nb.vo.UserVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.util.StringUtils;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {

//    void updateById(User user);

    default Page<UserVO> searchPage(Page<UserVO> page, UserQueryDTO queryDTO) {
        String userName = queryDTO.getUsername();
        String realName = queryDTO.getRealName();
        Integer roleId = queryDTO.getRoleId();
        Integer deptId = queryDTO.getDeptId();
        Integer status = queryDTO.getStatus();
        QueryWrapper queryWrapper = QueryWrapper.create();
        if (StringUtils.hasText(userName)) {
            queryWrapper.like("username", "%" + userName + "%");
        }
        // 真实姓名模糊查询
        if (StringUtils.hasText(realName)) {
            queryWrapper.like("real_name", "%" + realName + "%");
        }
        // 部门ID精确查询
        if (deptId != null) {
            queryWrapper.eq("dept_id", deptId);
        }
        if (roleId != null) {
            queryWrapper.eq("role_id", roleId);
        }
        // 状态精确查询
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        Page<UserVO> userVOPage1 = this.paginateWithRelationsAs(page, queryWrapper, UserVO.class);
        return userVOPage1;
    }

   default void updatePasswordById(Long id, String password){
        User user = new User();
        user.setId(id);
        user.setPassword(password);
        this.update(user);
   }
}
